

function genNumberByWeight(ids, weights)    //按权重生成数字
{
    let sum = weights.reduce((sum, weight) =>
    {
        return sum + weight
    }, 0)
    let axis = [], start = 0
    for (let i in ids)
    {
        axis.push({
            id: ids[i],
            end: start + weights[i] / sum
        })
        start += weights[i] / sum
    }
    console.log(axis)
    let X = Math.random() // X=[0,1)
    for (let interval of axis)
    {
        if (X < interval.end)
        {
            return interval.id
        }
    }
}
function randomNum(minNum, maxNum) //生成区间 [minNum,maxNum]间的数字
{
    return parseInt(Math.random() * (maxNum - minNum + 1) + minNum);
}

module.exports = {
    randomNum, genNumberByWeight
}